package com.ithzl.capacity.service.impl;

import com.ithzl.capacity.mapper.UserMapper;
import com.ithzl.capacity.pojo.User;
import com.ithzl.capacity.pojo.dto.UserPageDTO;
import com.ithzl.capacity.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    UserMapper userMapper;

    @Override
    public boolean changePassword(User user, String newPassword) {
        int id=user.getId();
        boolean out=false;
        try {
            userMapper.updatePasswordById(id,newPassword);
            out=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return out;
    }

    @Override
    public List<User> inquireUserList(UserPageDTO userPageDTO) {
        List<User> userList=userMapper.selectUserList(userPageDTO);
        return userList;
    }

    @Override
    public void addUser(User user) {
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        userMapper.addUser(user);
    }

    @Override
    public void amendUser(User user) {
        userMapper.update(user);
    }

    @Override
    public void deleteUser(int id) {
        userMapper.delete(id);
    }
}
